package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理接口")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation("新增")
    public Result add(@RequestParam List<String> checkgroupIds, @RequestBody Setmeal setMeal){
        log.info("新增套餐：{}",setMeal);
        setMealService.add(checkgroupIds,setMeal);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS,null);
    }

    /**
     * 编辑
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑")
    public Result edit(@RequestBody Setmeal setmeal,
                       @RequestParam(required = false) List<String> checkGroupIds) {
        setMealService.edit(setmeal,checkGroupIds);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS,null);
    }
    /**
     * 根据id查询套餐
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询套餐")
    public Result findById(@RequestParam Integer id){
        Setmeal setmeal = setMealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}", queryPageBean);
        PageResult pageResult = setMealService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 阿里云文件上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("阿里云文件上传")
    public Result uploadFile(@RequestParam MultipartFile imgFile) {
        log.info("文件上传");
        String url = setMealService.uploadFile(imgFile);
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,url);
    }
}
